
import {FileDAO} from '../../manage/dao/filedao.js'

import { LGXEnums } from '../common/sysenum.js';
import { KeyGenerator } from '../common/keygenerator.js';
import lgxcom from '../common/common.js'

var InitGraphObjSvc = {}

InitGraphObjSvc.initFrmFile = function (jsonData, datamanger) {
    var objList = FileDAO.parsingFileData(jsonData);
    this.parsing2Mem(objList, datamanger);
}

InitGraphObjSvc.initFrmStream = function (jsonData, datamanger) {
    let layerMng = datamanger.getLayerManager();
    let layArr = jsonData["layers"];
    if (layArr) {
        for (var idx in layArr) {
            let layerObj = layArr[idx];
            layerMng.addLayerByInfo(layerObj);
        }
    }

    var objList = [];
    let viewInfo = {
        scaling: -1, vleft: -1, vtop: -1, vright: -1, vbottom: -1,
        seed: 1000, backColor: 0, projectScale: 1
    };
    if (jsonData != "" && jsonData != null) {
        let result = FileDAO.parsingStreamData(jsonData);
        objList = result.dataList;
        viewInfo.scaling = result.scaling;
        viewInfo.vleft = result.vleft;
        viewInfo.vtop = result.vtop;
        viewInfo.vright = result.vright;
        viewInfo.vbottom = result.vbottom;
        viewInfo.seed = result.seed;
        viewInfo.backColor = result.backColor;
        viewInfo.projectScale = result.projectScale;
    }
    this.parsingGraph2Mem(objList, datamanger, viewInfo);
    this.makeTopo(jsonData, datamanger);
}

InitGraphObjSvc.parsingGraph2Mem = function (objList, datamanger, viewInfo) {
    let unit2GraphMap = [];
    for (var idx in objList) {
        let obj = objList[idx];
        let devFullType = obj.type;
        let pNewGraph = datamanger.createGraph4DataInit(obj.layerName, devFullType);
        if (pNewGraph) {
            if (obj.refSymbolUUID && obj.refSymbolUUID.length > 0) {
                let symbol = datamanger.getSymbol(obj.refSymbolUUID, 0);
                if (symbol) {
                    pNewGraph.setSymbol(symbol);
                }
            }
            else{
                let symbol = datamanger.getSymbolByType(obj.type, 0);
                if (symbol) {
                    pNewGraph.setSymbol(symbol);
                }
            }
            pNewGraph.setNotifyFlag(false);
            pNewGraph.assignInfo(obj);
            pNewGraph.setNotifyFlag(true);
            pNewGraph.calScope(1);
            unit2GraphMap.push({ unit: obj, graph: pNewGraph });
        }
    }
    if (viewInfo && viewInfo.scaling && viewInfo.scaling != -1) {
        let mapViewInfo = datamanger.getMapViewInfo();
        mapViewInfo.setScaling(viewInfo.scaling);
        let color = new lgxcom.LGXColor();
        color.setColorByValue(parseInt(viewInfo.backColor));
        mapViewInfo.setBackColor(color);
        mapViewInfo.setViewPort(viewInfo.vleft, viewInfo.vtop, viewInfo.vright, viewInfo.vbottom);
        mapViewInfo.setProjectScale(viewInfo.projectScale);
    }
    if (viewInfo) {
        KeyGenerator.init(viewInfo.seed);
    }
    this.setGraphContainerInfo(unit2GraphMap, datamanger);
    this.attchLabel2Graph(unit2GraphMap, datamanger);
}

InitGraphObjSvc.makeTopo = function (jsonData, datamanger) {
    if (jsonData.topodata) {
        let conductArr = jsonData.topodata.conductArr;
        let connlineArr = jsonData.topodata.connlineArr;
        let nodeArr = jsonData.topodata.nodeArr;
        this.createConductRes(conductArr, datamanger);
        let connLineLst = this.createConnlineRes(connlineArr, datamanger);
        this.createNodeRes(nodeArr, connLineLst, connlineArr, datamanger);
    }
}

InitGraphObjSvc.createConductRes = function (arr, dataManager) {
    let topoManager = dataManager.getTopoManager();
    let list = topoManager.initConduct(arr);
    for (let i = 0; i < list.length; i++) {
        let data = list[i];
        let info = arr[i];
        let refGraph = dataManager.findGraphByRID(info.refID);
        if (refGraph) {
            data.setRefGraph(refGraph);
        }
    }
}

InitGraphObjSvc.createConnlineRes = function (arr, dataManager) {
    let topoManager = dataManager.getTopoManager();
    let list = topoManager.initConnline(arr);
    for (let i = 0; i < list.length; i++) {
        let data = list[i];
        let info = arr[i];
        let refGraph = dataManager.findGraphByRID(info.refID);
        if (refGraph) {
            data.setRefGraph(refGraph);
        }
    }
    return list;
}

InitGraphObjSvc.createNodeRes = function (nodeInfoArr, connLineLst, connlineInfoArr, dataManager) {
    let topoManager = dataManager.getTopoManager();
    let nodeList = topoManager.initNode(nodeInfoArr);
    for (let i = 0; i < nodeList.length; i++) {
        let node = nodeList[i];
        let info = nodeInfoArr[i];
        let refGraph = dataManager.findGraphByRID(info.refID);
        if (refGraph) {
            let graphRes = refGraph.getTopoRes();
            if (graphRes) {
                node.setRefRes(graphRes);
                graphRes.addNode(node);
            }
        }
    }

    for (let i = 0; i < connLineLst.length; i++) {
        let lineInfo = connlineInfoArr[i];
        let lineRes = connLineLst[i];
        let node = this.findNode(lineInfo.stNodeID, nodeList);
        if (node) {
            lineRes.setStNode(node);
            node.addConnLine(lineRes);
        }

        node = this.findNode(lineInfo.endNodeID, nodeList);
        if (node) {
            lineRes.setEndNode(node);
            node.addConnLine(lineRes);
        }
    }
}

InitGraphObjSvc.findNode = function (nodeID, nodeList) {
    let destNode = null;
    if (nodeID != 0) {
        nodeList.forEach(node => {
            if (node.id == nodeID) {
                destNode = node;
                return;
            }
        });
    }
    return destNode;
}


InitGraphObjSvc.setGraphContainerInfo = function (unit2GraphMap, datamanger) {
    for (var idx in unit2GraphMap) {
        let info = unit2GraphMap[idx];
        if (info.unit.superID != 0) {
            let supGraph = datamanger.findGraph(info.unit.superID);
            if (supGraph) {
                supGraph.addChild(info.graph);
            }
        }
    }
}

InitGraphObjSvc.attchLabel2Graph = function (unit2GraphMap, datamanger) {
    for (var idx in unit2GraphMap) {
        let info = unit2GraphMap[idx];
        if (info.graph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
            && info.unit.parentGID != 0) {
            let dstGraph = datamanger.findGraph(info.unit.parentGID);
            if (dstGraph) {
                dstGraph.setLabel(info.graph);
                info.graph.resetLabelPos();
            }
        }
    }
}



export { InitGraphObjSvc }